package com.zmc.admin.controller.system;

import com.zmc.admin.infrastructure.bean.vo.config.SysConfigVo;
import com.zmc.admin.infrastructure.constant.ErrorCodeConstants;
import com.zmc.admin.infrastructure.entity.SysConfigEntity;
import com.zmc.admin.infrastructure.service.SysConfigService;
import com.zmc.admin.infrastructure.bean.query.SysConfigQuery;
import com.zmc.common.annotation.PreventRepeatSubmit;
import com.zmc.common.enums.RequestSourceEnum;
import com.zmc.common.exception.base.WebDisplayException;
import com.zmc.common.response.AjaxResult;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.R;
import com.zmc.common.utils.poi.ExcelUtil;
import com.zmc.common.utils.token.AuthTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.zmc.common.response.AjaxResult.success;

/**
 * 参数配置 信息操作处理
 * @author zmc
 */
@RestController
@RequestMapping("/config")
public class SysConfigController {
    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private AuthTokenService authTokenService;


    /**
     * 获取参数配置列表
     */
    @PostMapping("/list")
    public R list(@RequestBody SysConfigQuery sysConfigQuery) {
        PageUtils page = sysConfigService.queryPage(sysConfigQuery);
        return R.page(page);
    }

    @PostMapping("/export")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public void export(HttpServletResponse response, SysConfigQuery sysConfigQuery) {
        List<SysConfigEntity> list = sysConfigService.selectConfigList(sysConfigQuery);
        ExcelUtil<SysConfigEntity> util = new ExcelUtil<>(SysConfigEntity.class);
        util.exportExcel(response, list, "参数数据");
    }

    @PostMapping(value = "/info")
    public AjaxResult getInfo(@RequestBody SysConfigVo vo) {
        return success(sysConfigService.getById(vo.getConfigId()));
    }


    @PostMapping(value = "/configKey")
    public R getConfigKey(@RequestBody SysConfigVo vo) {
        return R.ok(sysConfigService.selectConfigConfigKey(vo.getConfigKey()));
    }

    @PostMapping("/add")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R add(@RequestBody SysConfigEntity config) {
        if (!sysConfigService.checkConfigKeyUnique(config)) {
            throw new WebDisplayException(ErrorCodeConstants.CONFIG_KEY_EXIT_EXCEPTION);
        }
        config.setCreateBy(authTokenService.getUserInfo().getUsername());
        sysConfigService.save(config);
        return R.ok();
    }

    @PostMapping("/update")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R edit(@RequestBody SysConfigEntity config) {
        if (!sysConfigService.checkConfigKeyUnique(config)) {
            throw new WebDisplayException(ErrorCodeConstants.CONFIG_KEY_EXIT_EXCEPTION);
        }
        config.setUpdateBy(authTokenService.getUserInfo().getUsername());
       sysConfigService.updateById(config);
        return R.ok();
    }

    @PostMapping("/delete")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R remove(@RequestBody SysConfigVo vo) {
        sysConfigService.deleteConfigById(vo.getConfigId());
        return R.ok();
    }

    @PostMapping("/refreshCache")
    public R refreshCache() {
        sysConfigService.resetConfigCache();
        return R.ok();
    }
}
